package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.geom.*;
	
	import mx.collections.*;
	
	public class PartialProcessingAction extends AbstractAction
	{
		private var temps:ArrayCollection = new ArrayCollection();
		private var currentIndex:int = 0;
		private var strength:Number;
		
		public function PartialProcessingAction()
		{
			this.temps.addItem(Mediator.getCurrentBitmapData());
		}
		
		public function setStrength(v:Number):void
		{
			this.strength = v;
		}
		
		public function addPartial(p:Array):void
		{
			Mediator.alert("Select region at center (" + (p[0] as Number).toFixed(0) +
					" , " + (p[0] as Number).toFixed(0) + ")"); 
			var cur:BitmapData = this.temps[this.currentIndex++];
			var processed:BitmapData = this.doParticalProcess(cur, p, this.strength);
			this.temps.addItem(processed);
			Mediator.getCurrentAquaImage().loadImage(processed);
		}
		
		public function previewPartial(p:Array = null):void
		{
			if(p != null)
			{
				var cur:BitmapData = this.temps[this.currentIndex];
				var processed:BitmapData = this.doParticalProcess(cur, p, this.strength);
				Mediator.getCurrentAquaImage().loadImage(processed);
			}
			else
			{
				Mediator.getCurrentAquaImage().loadImage(this.temps[this.currentIndex]);
			}
		}
		
		public function reset():void
		{
			this.temps.removeAll();
			this.temps.addItem(Mediator.getCurrentBitmapData());
			this.currentIndex = 0;
			Mediator.getCurrentAquaImage().loadImage();
		}
		
		public function undo():void
		{
			if(this.currentIndex > 0) 
			{
				this.temps.removeItemAt(this.currentIndex--);
				Mediator.getCurrentAquaImage().loadImage(this.temps[this.currentIndex]);
			}
		}
		
		protected virtual function doParticalProcess(cur:BitmapData, p:Array, str:Number):BitmapData
		{
			throw new Error("Unimplemented method of doParticalProcess");
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			return this.temps[this.currentIndex];
		}
	}
}